home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / cagd_lib / cbzr_pwr.c < prev    next >
C/C++ Source or Header  |  1995-12-30  |  9KB  |  203 lines

  1. /******************************************************************************
  2. * CBzr_Pwr.c - Bezier to pawer basis conversion.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Jun. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "cagd_loc.h"
  11.  
  12. static CagdRType BinomCoef(int n, int i);
  13.  
  14. /******************************************************************************
  15. * DESCRIPTION:                                                               M
  16. * Converts the given curve from Bezier basis functions to a Power basis      M
  17. * functions. Using:                                 M
  18. *                                         M
  19. *      n                                     V
  20. *      __                                     V
  21. *  n      \    j-i n    j   j                             V
  22. * B (t) = /  (-1)  ( ) ( ) t                             V
  23. *  i      --        j   i                             V
  24. *     j=i                                     V
  25. *                         n-i                 V
  26. * Which can be derived by expanding the (1-t)    term in bezier basis         V
  27. * function definition as:                             V
  28. *                                           V
  29. *        n-i                                   V
  30. *           __                                   V
  31. *      n-i  \  n-i      j                             V
  32. * (1-t)   = / (   ) (-t)    using binomial expansion.             V
  33. *        --  j                                 V
  34. *       j=0                                     V
  35. *                                                                M
  36. * This routine simply take the weight of each Bezier basis function B(t) and M
  37. * spread it into the different power basis t^j function scaled by:         M
  38. *                                                                M
  39. *    j-i n    j                                 V
  40. *   (-1)   ( ) ( )                                                          V
  41. *           j   i                                                  V
  42. *                                                                            *
  43. * PARAMETERS:                                                                M
  44. *   Crv:       To convert into Power basis function representation.          M
  45. *                                                                            *
  46. * RETURN VALUE:                                                              M
  47. *   CagdCrvStruct *:  Same geometry, but in the Power basis.                 M
  48. *                                                                            *
  49. * KEYWORDS:                                                                  M
  50. *   CnvrtBezier2PowerCrv, power basis, conversion                            M
  51. *****************************************************************************/
  52. CagdCrvStruct *CnvrtBezier2PowerCrv(CagdCrvStruct *Crv)
  53. {
  54.     CagdBType
  55.     IsNotRational = !CAGD_IS_RATIONAL_CRV(Crv);
  56.     int i, j, l,
  57.     n = Crv -> Length,
  58.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crv -> PType);
  59.     CagdRType *PwrP, *BzrP;
  60.     CagdCrvStruct
  61.     *NewCrv = CagdCrvNew(CAGD_CPOWER_TYPE, Crv -> PType, n);
  62.  
  63.     NewCrv -> Order = n;
  64.  
  65.     for (l = IsNotRational; l <= MaxCoord; l++) {
  66.     PwrP = NewCrv -> Points[l];
  67.     BzrP = Crv -> Points[l];
  68.     ZAP_MEM(PwrP, sizeof(CagdRType) * n);
  69.  
  70.     for (i = 0; i < n; i++) {
  71.         for (j = i; j < n; j++) {
  72.         PwrP[j] += BzrP[i] * BinomCoef(n, j) * BinomCoef(j, i) *
  73.                         (((j - i) & 0x01) ? -1 : 1);
  74.         }
  75.     }
  76.     }
  77.  
  78.     return NewCrv;
  79. }
  80.  
  81. /******************************************************************************
  82. * DESCRIPTION:                                                               M
  83. * Converts the given curve from Power basis functions to Bezier basis        M
  84. * functions. Using:                                 M
  85. *                                         M
  86. *      n    j                                     V
  87. *      __  ( )                                     V
  88. *   i  \    i     n                                 V
  89. *  t = /  ----- B (t)                                 V
  90. *      --   n     j                                 V
  91. *      j=i ( )                                     V
  92. *        i                                     V
  93. *                                                                M
  94. * This routine simply take the weight of each Power basis function t^i and   M
  95. * spread it into the different basis basis function B(t) scaled by:         M
  96. *                                                                M
  97. *     j   / n                                     V
  98. *    ( ) / ( )                                                          V
  99. *     i /   i                                                      V
  100. *                                                                            *
  101. * PARAMETERS:                                                                M
  102. *   Crv:        To convert to Bezier basis functions.                        M
  103. *                                                                            *
  104. * RETURN VALUE:                                                              M
  105. *   CagdCrvStruct *:   Same geometry, in the Bezier basis functions.         M
  106. *                                                                            *
  107. * KEYWORDS:                                                                  M
  108. *   CnvrtPower2BezierCrv, power basis, conversion                            M
  109. *****************************************************************************/
  110. CagdCrvStruct *CnvrtPower2BezierCrv(CagdCrvStruct *Crv)
  111. {
  112.     CagdBType
  113.     IsNotRational = !CAGD_IS_RATIONAL_CRV(Crv);
  114.     int i, j, l,
  115.     n = Crv -> Length,
  116.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crv -> PType);
  117.     CagdRType *PwrP, *BzrP;
  118.     CagdCrvStruct
  119.     *NewCrv = BzrCrvNew(n, Crv -> PType);
  120.  
  121.     for (l = IsNotRational; l <= MaxCoord; l++) {
  122.     PwrP = Crv -> Points[l];
  123.     BzrP = NewCrv -> Points[l];
  124.     ZAP_MEM(BzrP, sizeof(CagdRType) * n);
  125.  
  126.     for (i = 0; i < n; i++) {
  127.         for (j = i; j < n; j++) {
  128.         BzrP[j] += PwrP[i] * BinomCoef(j, i) / BinomCoef(n, i);
  129.         }
  130.     }
  131.     }
  132.  
  133.     return NewCrv;
  134. }
  135.  
  136. /*****************************************************************************
  137. * DESCRIPTION:                                                               *
  138. * Evaluate the following:                             *
  139. *             n         n!                         *
  140. *            ( ) = -------------                     *
  141. *             i    i! * (n - i)!                     *
  142. *                                                                            *
  143. * PARAMETERS:                                                                *
  144. *   n, i:    Coefficients of the binom.                                      *
  145. *                                                                            *
  146. * RETURN VALUE:                                                              *
  147. *   CagdRType:   Result in floating point form to prevent from overflows.    *
  148. *****************************************************************************/
  149. static CagdRType BinomCoef(int n, int i)
  150. {
  151.     int j;
  152.     CagdRType c = 1.0;
  153.  
  154.     if ((n >> 1) > i) {                /* i is less than half of n: */
  155.     for (j = n - i + 1; j <= n; j++) c *= j;
  156.     for (j = 2; j <= i; j++) c /= j;
  157.     }
  158.     else {
  159.     for (j = i + 1; j <= n; j++) c *= j;
  160.     for (j = 2; j <= n - i; j++) c /= j;
  161.     }
  162.  
  163.     return c;
  164. }
  165.  
  166. /*****************************************************************************
  167. * DESCRIPTION:                                                               M
  168. * Power to Bezier conversion from surfaces.                     M
  169. *                                                                            *
  170. * PARAMETERS:                                                                M
  171. *   Srf:       To convert into Power basis function representation.          M
  172. *                                                                            *
  173. * RETURN VALUE:                                                              M
  174. *   CagdSrfStruct *:  Same geometry, but in the Power basis.                 M
  175. *                                                                            *
  176. * KEYWORDS:                                                                  M
  177. *   CnvrtBezier2PowerSrf, power basis, conversion                            M
  178. *****************************************************************************/
  179. CagdSrfStruct *CnvrtBezier2PowerSrf(CagdSrfStruct *Srf)
  180. {
  181.     CAGD_FATAL_ERROR(CAGD_ERR_NOT_IMPLEMENTED);
  182.     return NULL;
  183. }
  184.  
  185. /*****************************************************************************
  186. * DESCRIPTION:                                                               M
  187. * Bezier to Power conversion from surfaces.                     M
  188. *                                                                            *
  189. * PARAMETERS:                                                                M
  190. *   Srf:       To convert into Bezier basis function representation.         M
  191. *                                                                            *
  192. * RETURN VALUE:                                                              M
  193. *   CagdSrfStruct *:  Same geometry, but in the Bezier basis.                M
  194. *                                                                            *
  195. * KEYWORDS:                                                                  M
  196. *   CnvrtPower2BezierSrf, power basis, conversion                            M
  197. *****************************************************************************/
  198. CagdSrfStruct *CnvrtPower2BezierSrf(CagdSrfStruct *Srf)
  199. {
  200.     CAGD_FATAL_ERROR(CAGD_ERR_NOT_IMPLEMENTED);
  201.     return NULL;
  202. }
  203.